<!-- 异常记录 -->
<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
      @row-del="rowDel"
    >
      <template
        slot="exceptionPic"
        slot-scope="{ row }"
        v-if="row.exceptionPic"
      >
        <el-image
          style="height: 50px; width: 50px; margin-right: 10px"
          fit="cover"
          :src="src"
          v-for="(src, index) in row.exceptionPic.split(';')"
          :key="index"
          :preview-src-list="row.exceptionPic.split(';')"
        >
        </el-image>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getRecordList, deleteRecord } from "@/api/exception/index";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: "auto",
        calcHeight: 30,
        tip: false,
        addBtn: false,
        editBtn: false,
        searchShow: false,
        searchMenuSpan: 6,
        border: true,
        labelWidth: "120",
        menuWidth: "250",
        column: [
          {
            label: "需求单号",
            prop: "demandOrderNumber",
            search: true,
          },
          {
            label: "运单号",
            prop: "shippingNoteNumber",
            search: true,
          },
          {
            label: "运单状态",
            prop: "waybillStatus",
            type: "select",
            props: {
              label: "value",
              value: "key",
            },
            search: true,
            dicUrl: `/api/blade-tms/common/SelectEnum?name=waybillStatus`,
          },
          {
            label: "异常类型",
            prop: "exceptionClass",
          },
          {
            label: "异常原因",
            prop: "abnormal",
          },
          {
            label: "异常时间",
            prop: "exceptionDate",
          },
          {
            label: "异常时间",
            type: "datetime",
            prop: "logDate",
            search: true,
            searchLabelWidth: "100",
            searchRange: true,
            hide: true,
            showColumn: false,
            defaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "请选择开始日期",
            endPlaceholder: "请选择结束日期",
          },
          {
            label: "异常备注",
            prop: "remark",
          },
          {
            label: "异常图片",
            prop: "exceptionPic",
          },
          {
            label: "上传人",
            prop: "operator",
          },
        ],
      },
      data: [],
    };
  },
  components: {},
  computed: {},
  methods: {
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      let query = Object.assign(params, this.query);
      if (query.logDate) {
        query = {
          ...query,
          abnormalTimeStarts: query.logDate ? query.logDate[0] : "",
          abnormalTimeEnd: query.logDate ? query.logDate[1] : "",
        };
        delete query.logDate;
      }
      this.loading = true;
      getRecordList(page.currentPage, page.pageSize, query).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.$nextTick(() => {
          this.$refs.crud.$refs.table.doLayout();
        });
      });
    },
    rowDel(form) {
      this.$confirm("是否确定删除所选异常记录?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteRecord({ ids: [form.id] }).then(
            () => {
              this.onLoad(this.page);
              this.$message({
                type: "success",
                message: "操作成功!",
              });
            },
            (error) => {
              window.console.log(error);
            }
          );
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss"></style>
